﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_RecvMoneyDetailSheet_New : DockContent
    {
        private DGBY.BLL.FinCheckInvoiceRecordDetailAuxiliaryTab_New m_bllInvoiceAuxiliary_New
    = new DGBY.BLL.FinCheckInvoiceRecordDetailAuxiliaryTab_New();

        private bool m_bSumStart = false;
        private DGBY.BLL.FactoryGoodsTab m_bllFactoryGoodsTab = new DGBY.BLL.FactoryGoodsTab();
        private DGBY.BLL.FinCheckInvoiceRecordDetailTab_New m_bllFinCheckInvoiceRecordDetailTab_New = new DGBY.BLL.FinCheckInvoiceRecordDetailTab_New();

        private DGBY.BLL.SJPackingTab m_bllSJPackingTab = new DGBY.BLL.SJPackingTab();
        private DGBY.BLL.FinRecvMoneyAuxiliaryTab_New m_bllFinRecvMoneyAuxiliaryTab_New = new DGBY.BLL.FinRecvMoneyAuxiliaryTab_New();
        private DGBY.BLL.FinDepRecvAndPayDetailTab_New m_bllFinDepRecvAndPayDetailTab_New = new DGBY.BLL.FinDepRecvAndPayDetailTab_New();
        private Hashtable m_SQLStringList = new Hashtable();
        private ArrayList m_arrayList = new ArrayList();
        private int m_nSelectRowIndex = 0;
        private DGBY.BLL.FinRecvMoneyDetailTab_New m_bllFinRecvMoneyDetailTab_New = new DGBY.BLL.FinRecvMoneyDetailTab_New();
     //   private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private DGBY.BLL.FinRecvMoneyWayTab m_bllFinRecvMoneyWayTab = new DGBY.BLL.FinRecvMoneyWayTab();
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private int m_nDelIndex = -1;
        private bool m_bInsertFlag = false;

        public Frm_RecvMoneyDetailSheet_New()
        {
            InitializeComponent();
        }
        private void ShowAllRecord(string strWhere)
        {                     
            DataSet ds = m_bllFinRecvMoneyDetailTab_New.GetList(strWhere);
            ultraGrid1.DataSource = null;

            DataSet dsTemp = m_bllBYCompanyTab.GetList("");

            this.ultraDropDownComp.DataSource = dsTemp.Tables[0];
            this.ultraDropDownComp.DisplayMember = "经营单位名称";
            this.ultraDropDownComp.ValueMember = "BYCompanyTabID";
            for (int i = 0; i < this.ultraDropDownComp.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownComp.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownComp.DisplayLayout.Bands[0].Columns["经营单位名称"].Hidden = false;

            dsTemp = m_bllFactoryDataTab.GetList("");

            this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFac.DisplayMember = "工厂简称";
            this.ultraDropDownFac.ValueMember = "FactoryDataTabID";
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["工厂简称"].Hidden = false;

            dsTemp = m_bllBiZhiTab.GetList("");

            this.ultraDropDownBiZhi.DataSource = dsTemp.Tables[0];
            this.ultraDropDownBiZhi.DisplayMember = "币制名称";
            this.ultraDropDownBiZhi.ValueMember = "BiZhiTabID";
            this.ultraDropDownBiZhi.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownBiZhi.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownBiZhi.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownBiZhi.DisplayLayout.Bands[0].Columns["币制名称"].Hidden = false;

            dsTemp = m_bllFinRecvMoneyWayTab.GetList("");

            this.ultraDropDownRecvWay.DataSource = dsTemp.Tables[0];
            this.ultraDropDownRecvWay.DisplayMember = "收款方式";
            this.ultraDropDownRecvWay.ValueMember = "FinRecvMoneyWayTabID";
            this.ultraDropDownRecvWay.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownRecvWay.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownRecvWay.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownRecvWay.DisplayLayout.Bands[0].Columns["收款方式"].Hidden = false;

            //this.ultraGrid1.DisplayLayout.Bands[0].Columns["结汇日期"].v = DateTime.Parse("1900-01-01");
          //  this.dateTimePickerRecHXDDate.Value = DateTime.Parse("1900-01-01");
          //  this.dateTimePickerAlreadyHXDate.Value = DateTime.Parse("1900-01-01");

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["经营公司"].ValueList = this.ultraDropDownComp;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["客户"].ValueList = this.ultraDropDownFac;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["币种"].ValueList = this.ultraDropDownBiZhi;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["收款方式"].ValueList = this.ultraDropDownRecvWay;


            if (Frm_Main.m_bCompleteVer == false)
            {
                foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                {
                    ugc.Hidden = true;
                }
                ultraGrid1.DisplayLayout.Bands[0].Columns["编号"].Hidden = false;
                ultraGrid1.DisplayLayout.Bands[0].Columns["经营公司"].Hidden = false;
                ultraGrid1.DisplayLayout.Bands[0].Columns["客户"].Hidden = false;
                ultraGrid1.DisplayLayout.Bands[0].Columns["付本日期"].Hidden = false;
                ultraGrid1.DisplayLayout.Bands[0].Columns["已付货款"].Hidden = false;
            }
            //this.ultraGrid1.Rows[this.ultraGrid1.Rows.Count - 1].Appearance.BackColor = Color.Aqua;
            //Infragistics.Win.UltraWinGrid.UltraGridColumn ultraGridColumn1
            //    = this.ultraGrid1.DisplayLayout.Bands[0].Columns["备注"];
            //this.ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].ValueList = 
            //this.ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].AutoCompleteMode = Infragistics.Win.AutoCompleteMode.None;
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            e.Layout.Bands[0].Columns["结汇日期"].Width = 120;
            e.Layout.Bands[0].Columns["结汇日期"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DateWithoutDropDown;
            //ClassUltraGridCommonSet.LocalPrintPreveiw();
            //ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "汇率" || str == "折算为USD" || str == "扣费后折算为USD" || str == "扣费后折算为HKD"
                    || str == "费用合计USD" || str == "折算为RMB"
                    || str == "已付货款" || str == "收付比率" || str == "原币金额"
                    || str == "编号" || str == "单据日期" || str == "子序号" || str == "是否已退税")

                {
                    column2.CellAppearance.BackColor = Color.Coral;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }

            InitColumnWidth();         
        }

        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["不参与退税"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["不参与对账"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["编号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["客户"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营公司"].Width = 160;
            ultraGrid1.DisplayLayout.Bands[0].Columns["结汇日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收款方式"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["币种"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["原币金额公式"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["原币金额"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["汇率公式"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["汇率"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["折算为USD"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["扣费后折算为USD"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["RMB汇率"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["折算为RMB"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["付本日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["已付货款"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["付税日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["已付税"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收付比率"].Width = 65;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否已退税"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收水单日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["手续费"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["利息"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关及拖车费"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["其他费用"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["费用合计USD"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["港币汇率"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["扣费后折算为HKD"].Width = 60;
        }


        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                  //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;   

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "经营公司" || strTemp == "客户" || strTemp == "币种" 
                            || strTemp == "收款方式")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;   

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "经营公司" || strTemp == "客户" 
                            || strTemp == "币种" || strTemp == "收款方式")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        
        }

        private void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
          //  CurrentUGR.Cells["单据日期"].Value = DateTime.Now.Date;
            CurrentUGR.Cells["结汇日期"].Value = DateTime.Now.Date.AddDays(-1);
            CurrentUGR.Cells["付本日期"].Value = DateTime.Now.Date;
            CurrentUGR.Cells["付税日期"].Value = DateTime.Now.Date;
            CurrentUGR.Cells["收水单日期"].Value = DateTime.Now.Date;
        //    CurrentUGR.Cells["经营公司"].Value = 1;
            CurrentUGR.Cells["不参与退税"].Value = 0;
            CurrentUGR.Cells["不参与对账"].Value = 0;
            CurrentUGR.Cells["客户"].Value = 297;
            CurrentUGR.Cells["币种"].Value = 1;
            CurrentUGR.Cells["收款方式"].Value = 4;
            CurrentUGR.Cells["经营公司"].Value = 3;
            CurrentUGR.Cells["编号"].Value = MakeRecvMoneyDetailDJNo();
            CurrentUGR.Cells["原币金额"].Value = 0;
            CurrentUGR.Cells["汇率"].Value = 0;
            CurrentUGR.Cells["RMB汇率"].Value = 0;
            CurrentUGR.Cells["港币汇率"].Value = 0;
            CurrentUGR.Cells["汇率公式"].Value ="";
            CurrentUGR.Cells["手续费"].Value = 0;
            CurrentUGR.Cells["利息"].Value = 0;
            CurrentUGR.Cells["报关及拖车费"].Value = 0;
            CurrentUGR.Cells["其他费用"].Value = 0;
            CurrentUGR.Cells["已付税"].Value = 0;
         //   CurrentUGR.Cells["子序号"].Value = 0;
            CurrentUGR.Cells["是否已退税"].Value = 0;
            CurrentUGR.Cells["备注"].Value = "";
        //    CurrentUGR.Cells["付税编号"].Value = "";

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }
           
        }

        private Int64 MakePayMoneyDetailDJNo()
        {
            string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');
            DataSet ds = m_bllFinDepRecvAndPayDetailTab_New.GetSJPackDJNoByDate("DanJuDate = '" + strDate + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MaxJPackDJNum"].ToString();
                long nDJNo = 0;
                if (strDJNo != "")
                    nDJNo = Convert.ToInt64(strDJNo, 10);
                nDJNo++;
                string strTemp = "";
                if (strDJNo != "")
                    strTemp = string.Format("{0:d4}", nDJNo);
                else
                    strTemp = strDate + string.Format("{0:d4}", nDJNo);

                return Convert.ToInt64(strTemp, 10);
            }

            return 0;
        }

        private Int64 MakeRecvMoneyDetailDJNo()
        {
            string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');
            DataSet ds = m_bllFinRecvMoneyDetailTab_New.GetSJPackDJNoByDate("DanJuDate = '" + strDate + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MaxJPackDJNum"].ToString();
                long nDJNo = 0;
                if (strDJNo != "")
                    nDJNo = Convert.ToInt64(strDJNo, 10);
                nDJNo++;
                string strTemp = "";
                if (strDJNo != "")
                    strTemp = string.Format("{0:d4}", nDJNo);
                else
                    strTemp = strDate + string.Format("{0:d4}", nDJNo);

                return Convert.ToInt64(strTemp, 10);
            }

            return 0;
        }

        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            InitNewAddRowValue(e.Row.Index);

        }

       
        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New = new DGBY.Model.FinRecvMoneyDetailTab_New();


                modelFinRecvMoneyDetailTab_New.isNotJoinBackTax = Convert.ToBoolean(CurrentUGR.Cells["不参与退税"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.isNotJoinCheckMoney = Convert.ToBoolean(CurrentUGR.Cells["不参与对账"].Value.ToString());
        
                modelFinRecvMoneyDetailTab_New.BianHao = CurrentUGR.Cells["编号"].Value.ToString();
                modelFinRecvMoneyDetailTab_New.PayTaxBianHao = "";//原币金额公式
                modelFinRecvMoneyDetailTab_New.Note = CurrentUGR.Cells["备注"].Value.ToString();

                DateTime dtTemp = (DateTime)CurrentUGR.Cells["结汇日期"].Value;
                modelFinRecvMoneyDetailTab_New.JieHuiDate = dtTemp.Date;
                modelFinRecvMoneyDetailTab_New.YuanBiMoneyValue = Convert.ToDecimal(CurrentUGR.Cells["原币金额"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.HuiLv = Convert.ToDecimal(CurrentUGR.Cells["汇率"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.RMBHuiLv = Convert.ToDecimal(CurrentUGR.Cells["RMB汇率"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.HKHuiLv = Convert.ToDecimal(CurrentUGR.Cells["港币汇率"].Value.ToString());
                dtTemp = (DateTime)CurrentUGR.Cells["付本日期"].Value;
                modelFinRecvMoneyDetailTab_New.PayBenDate = dtTemp.Date;
                dtTemp = (DateTime)CurrentUGR.Cells["付税日期"].Value;
                modelFinRecvMoneyDetailTab_New.PayTaxDate = dtTemp.Date;
                dtTemp = (DateTime)CurrentUGR.Cells["收水单日期"].Value;
                modelFinRecvMoneyDetailTab_New.RevcShuiDanDate = dtTemp.Date;
           //     modelFinRecvMoneyDetailTab_New.BYCompanyTabID = Convert.ToInt32(CurrentUGR.Cells["经营公司"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.BiZhiTabID = Convert.ToInt32(CurrentUGR.Cells["币种"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.FactoryDataTabID = Convert.ToInt32(CurrentUGR.Cells["客户"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.FinRecvMoneyWayTabID = Convert.ToInt32(CurrentUGR.Cells["收款方式"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.BYCompanyTabID = Convert.ToInt32(CurrentUGR.Cells["经营公司"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.HuiLvGongShi = CurrentUGR.Cells["汇率公式"].Value.ToString();
                modelFinRecvMoneyDetailTab_New.ShouxuFee = Convert.ToDecimal(CurrentUGR.Cells["手续费"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.LiXi = Convert.ToDecimal(CurrentUGR.Cells["利息"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.BaoguanFee = Convert.ToDecimal(CurrentUGR.Cells["报关及拖车费"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.OtherFee = Convert.ToDecimal(CurrentUGR.Cells["其他费用"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.PayTax = Convert.ToDecimal(CurrentUGR.Cells["已付税"].Value.ToString());
                //dtTemp = (DateTime)CurrentUGR.Cells["单据日期"].Value;
                modelFinRecvMoneyDetailTab_New.DanJuDate = DateTime.Now.Date; 
                modelFinRecvMoneyDetailTab_New.SubSeqNo = 0;
                modelFinRecvMoneyDetailTab_New.IsPayTaxDone = Convert.ToInt32(CurrentUGR.Cells["是否已退税"].Value.ToString(), 10);

               

                int nId = m_bllFinRecvMoneyDetailTab_New.Add(modelFinRecvMoneyDetailTab_New);
                Frm_Main.Log2DB(this.Text, "增加", modelFinRecvMoneyDetailTab_New.BianHao);
                ShowAllRecord("FinRecvMoneyDetailTab_NewID = '" + nId.ToString() + "'");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private Decimal ComputeKouFeeValue(DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New)
        {
            Decimal dtTemp = 0;
            DataSet dsFac = m_bllFinRecvMoneyDetailTab_New.GetFacInfoList("FinRecvMoneyDetailTab_Newid = '" + modelFinRecvMoneyDetailTab_New.FinRecvMoneyDetailTab_NewID + "'");
            bool bIsHkFac = (bool)dsFac.Tables[0].Rows[0]["是否港币出货工厂"];
            
            if (bIsHkFac)
            {
                dtTemp
                    = ComputeKouFeeHkdEx(modelFinRecvMoneyDetailTab_New);
            }
            else
            {
                dtTemp
                    = ComputeKouFeeUsdEx(modelFinRecvMoneyDetailTab_New);
            }
            return dtTemp;
        }
        //,case when hkHuiLv > 0 then cast( ((YuanBiMoneyValue*RMBHuiLv - (ShouxuFee + LiXi+ BaoguanFee+ OtherFee))/hkHuiLv) AS NUMERIC(18,2)) else 0 end 扣费后折算为HKD

        private Decimal ComputeKouFeeHkdEx(DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New)
        {
            //string strnum = modelFinRecvMoneyDetailTab_New.HuiLv.ToString();
            //int n = strnum.IndexOf(".");
            //if (n + 11 < strnum.Length)
            //{
            //    string strTemp = strnum.Remove(n + 11);//小数点后6位
            //    modelFinRecvMoneyDetailTab_New.HuiLv = Convert.ToDecimal(strTemp);
            //}

            Decimal dtTempTotal = 0;
            //if(modelFinRecvMoneyDetailTab_New.BiZhiTabID == 5  && modelFinRecvMoneyDetailTab_New.HuiLv > 0)
            //    dtTempTotal = modelFinRecvMoneyDetailTab_New.YuanBiMoneyValue/ modelFinRecvMoneyDetailTab_New.HuiLv;
            //else if(modelFinRecvMoneyDetailTab_New.BiZhiTabID != 5)
            if (modelFinRecvMoneyDetailTab_New.HKHuiLv > 0)
                dtTempTotal = modelFinRecvMoneyDetailTab_New.YuanBiMoneyValue * modelFinRecvMoneyDetailTab_New.RMBHuiLv / modelFinRecvMoneyDetailTab_New.HKHuiLv;
            else
                dtTempTotal = 0;
            
            //else
            //    dtTempTotal = 0;

            Decimal dtTemp = 0;
            //if(modelFinRecvMoneyDetailTab_New.BiZhiTabID == 1 && modelFinRecvMoneyDetailTab_New.RMBHuiLv > 0)
            //    dtTemp = (modelFinRecvMoneyDetailTab_New.ShouxuFee + modelFinRecvMoneyDetailTab_New.LiXi+ modelFinRecvMoneyDetailTab_New.BaoguanFee+ modelFinRecvMoneyDetailTab_New.OtherFee)/modelFinRecvMoneyDetailTab_New.RMBHuiLv; 
            //else if(modelFinRecvMoneyDetailTab_New.BiZhiTabID == 5 && modelFinRecvMoneyDetailTab_New.HuiLv > 0)
            //    dtTemp = (modelFinRecvMoneyDetailTab_New.ShouxuFee + modelFinRecvMoneyDetailTab_New.LiXi+ modelFinRecvMoneyDetailTab_New.BaoguanFee+ modelFinRecvMoneyDetailTab_New.OtherFee)/modelFinRecvMoneyDetailTab_New.HuiLv; 
            //else if(modelFinRecvMoneyDetailTab_New.BiZhiTabID != 1 && modelFinRecvMoneyDetailTab_New.BiZhiTabID != 5 && modelFinRecvMoneyDetailTab_New.RMBHuiLv > 0)
            if (modelFinRecvMoneyDetailTab_New.HKHuiLv > 0)
                dtTemp = (modelFinRecvMoneyDetailTab_New.ShouxuFee + modelFinRecvMoneyDetailTab_New.LiXi + modelFinRecvMoneyDetailTab_New.BaoguanFee + modelFinRecvMoneyDetailTab_New.OtherFee) / modelFinRecvMoneyDetailTab_New.HKHuiLv;
            else
                dtTemp = 0;


            return (dtTempTotal - dtTemp);
        }
        private Decimal ComputeKouFeeUsdEx(DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New)
        {
            string strnum = modelFinRecvMoneyDetailTab_New.HuiLv.ToString();
            int n = strnum.IndexOf(".");
            if (n + 11 < strnum.Length)
            {
                string strTemp = strnum.Remove(n + 11);//小数点后6位
                modelFinRecvMoneyDetailTab_New.HuiLv = Convert.ToDecimal(strTemp);
            }

            Decimal dtTempTotal = 0;
            //if(modelFinRecvMoneyDetailTab_New.BiZhiTabID == 5  && modelFinRecvMoneyDetailTab_New.HuiLv > 0)
            //    dtTempTotal = modelFinRecvMoneyDetailTab_New.YuanBiMoneyValue/ modelFinRecvMoneyDetailTab_New.HuiLv;
            //else if(modelFinRecvMoneyDetailTab_New.BiZhiTabID != 5)
            dtTempTotal = modelFinRecvMoneyDetailTab_New.YuanBiMoneyValue * modelFinRecvMoneyDetailTab_New.HuiLv;
            //else
            //    dtTempTotal = 0;
           
            Decimal dtTemp = 0;
            //if(modelFinRecvMoneyDetailTab_New.BiZhiTabID == 1 && modelFinRecvMoneyDetailTab_New.RMBHuiLv > 0)
            //    dtTemp = (modelFinRecvMoneyDetailTab_New.ShouxuFee + modelFinRecvMoneyDetailTab_New.LiXi+ modelFinRecvMoneyDetailTab_New.BaoguanFee+ modelFinRecvMoneyDetailTab_New.OtherFee)/modelFinRecvMoneyDetailTab_New.RMBHuiLv; 
            //else if(modelFinRecvMoneyDetailTab_New.BiZhiTabID == 5 && modelFinRecvMoneyDetailTab_New.HuiLv > 0)
            //    dtTemp = (modelFinRecvMoneyDetailTab_New.ShouxuFee + modelFinRecvMoneyDetailTab_New.LiXi+ modelFinRecvMoneyDetailTab_New.BaoguanFee+ modelFinRecvMoneyDetailTab_New.OtherFee)/modelFinRecvMoneyDetailTab_New.HuiLv; 
            //else if(modelFinRecvMoneyDetailTab_New.BiZhiTabID != 1 && modelFinRecvMoneyDetailTab_New.BiZhiTabID != 5 && modelFinRecvMoneyDetailTab_New.RMBHuiLv > 0)
            if (modelFinRecvMoneyDetailTab_New.RMBHuiLv > 0)
                dtTemp = (modelFinRecvMoneyDetailTab_New.ShouxuFee + modelFinRecvMoneyDetailTab_New.LiXi + modelFinRecvMoneyDetailTab_New.BaoguanFee + modelFinRecvMoneyDetailTab_New.OtherFee) * modelFinRecvMoneyDetailTab_New.HuiLv / modelFinRecvMoneyDetailTab_New.RMBHuiLv;
            else
                dtTemp = 0;


            return (dtTempTotal - dtTemp);
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                m_SQLStringList.Clear();
                m_arrayList.Clear();
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New = m_bllFinRecvMoneyDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                if (modelFinRecvMoneyDetailTab_New.IsPayTaxDone > 0)
                {
                    MessageBox.Show("已退税，不能修改记录？", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                    
                }

                //Decimal dtOldYuanBiMoneyValue = ComputeKouFeeValue(modelFinRecvMoneyDetailTab_New);

                DataSet dsAuxiliary = m_bllFinRecvMoneyAuxiliaryTab_New.GetList("FinRecvMoneyDetailTab_NewID ='" + nSelectFinRecvMoneyDetailTab_NewID.ToString() + "'");
                int nCount = dsAuxiliary.Tables[0].Rows.Count;

                //modelFinRecvMoneyDetailTab_New.BianHao = CurrentUGR.Cells["编号"].Value.ToString();
                modelFinRecvMoneyDetailTab_New.PayTaxBianHao = CurrentUGR.Cells["原币金额公式"].Value.ToString(); ;
                modelFinRecvMoneyDetailTab_New.Note = CurrentUGR.Cells["备注"].Value.ToString();
                DateTime dtTemp = (DateTime)CurrentUGR.Cells["结汇日期"].Value;
                modelFinRecvMoneyDetailTab_New.JieHuiDate = dtTemp.Date;
                if (modelFinRecvMoneyDetailTab_New.PayTaxBianHao != "")
                    modelFinRecvMoneyDetailTab_New.YuanBiMoneyValue = this.ultraCalcManager1.Calculate(CurrentUGR.Cells["原币金额公式"].Value.ToString()).ToDecimal();
                else
                    modelFinRecvMoneyDetailTab_New.YuanBiMoneyValue = 0;
               // modelFinRecvMoneyDetailTab_New.YuanBiMoneyValue = Convert.ToDecimal(CurrentUGR.Cells["原币金额"].Value.ToString());
                //modelFinRecvMoneyDetailTab_New.HuiLv = Convert.ToDecimal(CurrentUGR.Cells["汇率"].Value.ToString());
                //modelFinRecvMoneyDetailTab_New.RMBHuiLv = Convert.ToDecimal(CurrentUGR.Cells["RMB汇率"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.isNotJoinBackTax = Convert.ToBoolean(CurrentUGR.Cells["不参与退税"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.isNotJoinCheckMoney = Convert.ToBoolean(CurrentUGR.Cells["不参与对账"].Value.ToString());
                dtTemp = (DateTime)CurrentUGR.Cells["付本日期"].Value;
                modelFinRecvMoneyDetailTab_New.PayBenDate = dtTemp.Date;
                dtTemp = (DateTime)CurrentUGR.Cells["付税日期"].Value;
                modelFinRecvMoneyDetailTab_New.PayTaxDate = dtTemp.Date;
                dtTemp = (DateTime)CurrentUGR.Cells["收水单日期"].Value;
                modelFinRecvMoneyDetailTab_New.RevcShuiDanDate = dtTemp.Date;
               // modelFinRecvMoneyDetailTab_New.BYCompanyTabID = Convert.ToInt32(CurrentUGR.Cells["经营公司"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.BiZhiTabID = Convert.ToInt32(CurrentUGR.Cells["币种"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.FactoryDataTabID = Convert.ToInt32(CurrentUGR.Cells["客户"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.FinRecvMoneyWayTabID = Convert.ToInt32(CurrentUGR.Cells["收款方式"].Value.ToString(), 10);
                modelFinRecvMoneyDetailTab_New.HKHuiLv = Convert.ToDecimal(CurrentUGR.Cells["港币汇率"].Value.ToString());
                string strBGH = CurrentUGR.Cells["经营公司"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelFinRecvMoneyDetailTab_New.BYCompanyTabID = Convert.ToInt32(CurrentUGR.Cells["经营公司"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBYCompanyTab.GetList("CompanyName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFinRecvMoneyDetailTab_New.BYCompanyTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BYCompanyTabID"].ToString(), 10);
                    }
                }
                modelFinRecvMoneyDetailTab_New.HuiLvGongShi = CurrentUGR.Cells["汇率公式"].Value.ToString();
                if (modelFinRecvMoneyDetailTab_New.HuiLvGongShi != "")
                    modelFinRecvMoneyDetailTab_New.HuiLv = this.ultraCalcManager1.Calculate(CurrentUGR.Cells["汇率公式"].Value.ToString()).ToDecimal();
                else
                    modelFinRecvMoneyDetailTab_New.HuiLv = 0;
                
                modelFinRecvMoneyDetailTab_New.ShouxuFee = Convert.ToDecimal(CurrentUGR.Cells["手续费"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.LiXi = Convert.ToDecimal(CurrentUGR.Cells["利息"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.BaoguanFee = Convert.ToDecimal(CurrentUGR.Cells["报关及拖车费"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.OtherFee = Convert.ToDecimal(CurrentUGR.Cells["其他费用"].Value.ToString());
                modelFinRecvMoneyDetailTab_New.PayTax = Convert.ToDecimal(CurrentUGR.Cells["已付税"].Value.ToString());

                //这里折算成了usd，实际上是想折算成发票的币制
                Decimal dtNewUsd = ComputeKouFeeValue(modelFinRecvMoneyDetailTab_New);

                if (nCount == 0
                    && dtNewUsd > 0
                    && modelFinRecvMoneyDetailTab_New.IsPayTaxDone == 0)//第一次修改原币金额
                {
                    DGBY.Model.FinRecvMoneyAuxiliaryTab_New modelFinRecvMoneyAuxiliaryTab_New = new DGBY.Model.FinRecvMoneyAuxiliaryTab_New();
                    modelFinRecvMoneyAuxiliaryTab_New.FinRecvMoneyDetailTab_NewID = modelFinRecvMoneyDetailTab_New.FinRecvMoneyDetailTab_NewID;
                    modelFinRecvMoneyAuxiliaryTab_New.IsPayTaxDone = 0;
                    modelFinRecvMoneyAuxiliaryTab_New.Note = "";
                    modelFinRecvMoneyAuxiliaryTab_New.PayTaxDate = modelFinRecvMoneyDetailTab_New.PayTaxDate;
                    modelFinRecvMoneyAuxiliaryTab_New.SubSeqNo = 0;
                    modelFinRecvMoneyAuxiliaryTab_New.PayTaxMoneyValue = dtNewUsd;
                    modelFinRecvMoneyAuxiliaryTab_New.PayedTaxMoneyValue = 0;
                    m_bllFinRecvMoneyAuxiliaryTab_New.TransAdd(modelFinRecvMoneyAuxiliaryTab_New
                        , m_SQLStringList, m_arrayList, 0);

                    //string strMessage = string.Format("m_bllFinRecvMoneyAuxiliaryTab_New.TransAdd, ID is {0}"
                    //    , modelFinRecvMoneyDetailTab_New.FinRecvMoneyDetailTab_NewID.ToString());

                    //Frm_Main.LogInfo(strMessage);
                }
                if (nCount > 0
    && dtNewUsd > 0
    && modelFinRecvMoneyDetailTab_New.IsPayTaxDone == 0)//非第一次修改原币金额
                {
                    DataSet ds = m_bllFinRecvMoneyAuxiliaryTab_New.GetList("FinRecvMoneyDetailTab_NewID = '"
                        + modelFinRecvMoneyDetailTab_New.FinRecvMoneyDetailTab_NewID.ToString() + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        int nID = Convert.ToInt32(ds.Tables[0].Rows[0]["FinRecvMoneyAuxiliaryTab_NewID"].ToString(), 10);
                        DGBY.Model.FinRecvMoneyAuxiliaryTab_New modelFinRecvMoneyAuxiliaryTab_New = m_bllFinRecvMoneyAuxiliaryTab_New.GetModel(nID);
                        modelFinRecvMoneyAuxiliaryTab_New.PayTaxDate = modelFinRecvMoneyDetailTab_New.PayTaxDate;
                        modelFinRecvMoneyAuxiliaryTab_New.PayTaxMoneyValue = dtNewUsd;
                        modelFinRecvMoneyAuxiliaryTab_New.PayedTaxMoneyValue = 0;
                        m_bllFinRecvMoneyAuxiliaryTab_New.TransUpdate(modelFinRecvMoneyAuxiliaryTab_New
                            , m_SQLStringList, m_arrayList, 0);

    //                    string strMessage = string.Format("m_bllFinRecvMoneyAuxiliaryTab_New.TransUpdate, ID is {0}"
    //, modelFinRecvMoneyAuxiliaryTab_New.FinRecvMoneyDetailTab_NewID.ToString());
    //                    Frm_Main.LogInfo(strMessage);
                    }
           
                  
                }

    //            string strMessage2 = string.Format("m_bllFinRecvMoneyDetailTab_New.TransUpdate, ID is {0}"
    //, nSelectFinRecvMoneyDetailTab_NewID.ToString());
                        
    //            Frm_Main.LogInfo(strMessage2);
                
                m_bllFinRecvMoneyDetailTab_New.TransUpdate(modelFinRecvMoneyDetailTab_New
                    , m_SQLStringList, m_arrayList, 0);
                //ShowAllRecord("");
                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                //DataSet dsTemp = m_bllFinRecvMoneyDetailTab_New.GetList("FinRecvMoneyDetailTab_Newid = '" + nSelectFinRecvMoneyDetailTab_NewID + "'");
                //CurrentUGR.Cells["RMB汇率"].Value = (Decimal)dsTemp.Tables[0].Rows[0]["RMB汇率"];
                
               // Frm_Main.LogInfo("m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList)");
                Frm_Main.Log2DB(this.Text, "修改", modelFinRecvMoneyDetailTab_New.BianHao);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;



                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_NewOld = m_bllFinRecvMoneyDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                if (modelFinRecvMoneyDetailTab_NewOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllFinRecvMoneyDetailTab_New.Delete(nSelectFinRecvMoneyDetailTab_NewID);
                        Frm_Main.Log2DB(this.Text, "删除", modelFinRecvMoneyDetailTab_NewOld.BianHao);
                      

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;

            if (m_bSumStart)
            {
                if (this.ultraGrid1.ActiveCell.Appearance.BackColor.Name == "0")
                {
                    this.ultraGrid1.ActiveCell.Appearance.BackColor = Color.Aqua;
                }
                else
                {
                    this.ultraGrid1.ActiveCell.Appearance.BackColor = Color.FromName("0");
                }
            }

        }

        private void ultraGrid1_AfterRowCancelUpdate(object sender, RowEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void ultraGrid1_BeforeCellCancelUpdate(object sender, CancelableCellEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                //  row.Cells[2] = DateTime.Parse("1900-01-01");
            }
        }

        private void toolStripMenuItemDel_Click(object sender, EventArgs e)
        {
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                if (ultraGrid1.ActiveRow != null)
                {
                    UltraGridRow CurrentUGR;
                    CurrentUGR = ultraGrid1.Rows[nIndex];
                    int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                    DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New = m_bllFinRecvMoneyDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);
                    if (modelFinRecvMoneyDetailTab_New.IsPayTaxDone > 0)
                    {
                        MessageBox.Show("已付税不能删除!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    ultraGrid1.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void SetFormula(Control control, string formula)
        {
            // To acces extender the CalcManager extender properties, call 
            // the first get the GetCalcSettings method.
            CalcSettings objCalcSettings = null;
            objCalcSettings = this.ultraCalcManager1.GetCalcSettings(control);

            // Set the formula. 
            objCalcSettings.Formula = formula;
        }
      

        private void textBoxHuiLvValue_TextChanged(object sender, EventArgs e)
        {

            UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nSelectRowIndex];
            CurrentUGR.Cells["汇率"].Value = Convert.ToDecimal(this.textBoxHuiLvValue.Text);
            ModifyGoodsDetail(m_nSelectRowIndex);

        }

        private void ultraGrid1_AfterCellUpdate_1(object sender, CellEventArgs e)
        {
            if (e.Cell.Row.Index < 0)
                return;
            if (e.Cell.Column.Key == "汇率公式")
            {
                //modelFinInvoiceRecordTab_NewOld.FactorA = this.ultraCalcManager1.Calculate(this.textBoxFactorAFormula.Text).ToDecimal();
                m_nSelectRowIndex = e.Cell.Row.Index;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nSelectRowIndex];
                this.textBoxHuiLvFormula.Text = e.Cell.Value.ToString();
            }
            else if (e.Cell.Column.Key == "客户")
            {
                m_nSelectRowIndex = e.Cell.Row.Index;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nSelectRowIndex];
                
                string strID = e.Cell.Value.ToString();


                DataSet ds = m_bllFactoryDataTab.GetListEx("FactoryDataTab.FactoryDataTabID = '" + strID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {

                    CurrentUGR.Cells["经营公司"].Value = (int)ds.Tables[0].Rows[0]["BYCompanyTabID"];
                  

                }
            }
            else if (e.Cell.Column.Key == "原币金额公式")
            {
                m_nSelectRowIndex = e.Cell.Row.Index;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nSelectRowIndex];
                this.textBoxYuanBiFormula.Text = e.Cell.Value.ToString();
            }
            else if (e.Cell.Column.Key == "RMB汇率" && !m_bInsertFlag)
            {

                m_nSelectRowIndex = e.Cell.Row.Index;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nSelectRowIndex];
                Decimal dJinE = Convert.ToDecimal(e.Cell.Value.ToString());
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New = m_bllFinRecvMoneyDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                //if(modelFinRecvMoneyDetailTab_New.FinRecvMoneyWayTabID
                DataSet dsTemp = m_bllFactoryDataTab.GetList("FactoryDataTabid = '" + modelFinRecvMoneyDetailTab_New.FactoryDataTabID + "'");
                Decimal dtDiscountHuilv = (Decimal)dsTemp.Tables[0].Rows[0]["优惠汇率"];
                if(modelFinRecvMoneyDetailTab_New.BiZhiTabID == 1)//美元
                    modelFinRecvMoneyDetailTab_New.RMBHuiLv = (dtDiscountHuilv + 1) * dJinE;
                else if (modelFinRecvMoneyDetailTab_New.BiZhiTabID == 2)//港币
                    modelFinRecvMoneyDetailTab_New.RMBHuiLv = dtDiscountHuilv +  dJinE;
                else
                    modelFinRecvMoneyDetailTab_New.RMBHuiLv = dJinE;
                if (modelFinRecvMoneyDetailTab_New.BiZhiTabID == 2)
                {
                    modelFinRecvMoneyDetailTab_New.HKHuiLv = modelFinRecvMoneyDetailTab_New.RMBHuiLv;
                    CurrentUGR.Cells["港币汇率"].Value = modelFinRecvMoneyDetailTab_New.HKHuiLv;
                }
                m_bllFinRecvMoneyDetailTab_New.Update(modelFinRecvMoneyDetailTab_New);
              //  CurrentUGR.Cells["RMB汇率"].Value = dtDiscountHuilv + dJinE;
            }
        }

        private void textBoxHuiLvFormula_TextChanged(object sender, EventArgs e)
        {
            SetFormula(this.textBoxHuiLvValue, this.textBoxHuiLvFormula.Text);

        }

        private void textBoxYuanBiFormula_TextChanged(object sender, EventArgs e)
        {
            SetFormula(this.textBoxYuanBiValue, this.textBoxYuanBiFormula.Text);
        }

        private void textBoxYuanBiValue_TextChanged(object sender, EventArgs e)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nSelectRowIndex];
            CurrentUGR.Cells["原币金额"].Value = Convert.ToDecimal(this.textBoxYuanBiValue.Text);
            ModifyGoodsDetail(m_nSelectRowIndex);
        }

        private void Frm_RecvMoneyDetailSheet_New_Load(object sender, EventArgs e)
        {
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
            if (Frm_Main.m_bCompleteVer == false)
            {
                foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                {
                    ugc.Hidden = true;
                }
                ultraGrid1.DisplayLayout.Bands[0].Columns["编号"].Hidden = false;
                ultraGrid1.DisplayLayout.Bands[0].Columns["经营公司"].Hidden = false;
                ultraGrid1.DisplayLayout.Bands[0].Columns["客户"].Hidden = false;
                ultraGrid1.DisplayLayout.Bands[0].Columns["付本日期"].Hidden = false;
                ultraGrid1.DisplayLayout.Bands[0].Columns["已付货款"].Hidden = false;
            }
        }


        private void ProducePayBenMoney(bool bMessage, int nSelectFinRecvMoneyDetailTab_NewID)
        {
            try
            {

         

                DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_NewOld = m_bllFinRecvMoneyDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                DataSet ds = m_bllFinDepRecvAndPayDetailTab_New.GetList("FinDepRecvAndPayDetailTab_New.RecvBianNo = '" + modelFinRecvMoneyDetailTab_NewOld.BianHao.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    if(bMessage)
                        MessageBox.Show("已经生成了付款本金或者付款约定本金或者付款价税合计", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }


                DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New = new DGBY.Model.FinDepRecvAndPayDetailTab_New();
                modelFinDepRecvAndPayDetailTab_New.RecvBianNo = modelFinRecvMoneyDetailTab_NewOld.BianHao;
                modelFinDepRecvAndPayDetailTab_New.FinDepRecvAndPayDetailTab_NewBianNo = MakePayMoneyDetailDJNo().ToString();
                modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID = modelFinRecvMoneyDetailTab_NewOld.FactoryDataTabID;
                modelFinDepRecvAndPayDetailTab_New.FinRecvMoneyWayTabID = modelFinRecvMoneyDetailTab_NewOld.FinRecvMoneyWayTabID;
                modelFinDepRecvAndPayDetailTab_New.FinAbstractTabID = 1;//付款本金
                modelFinDepRecvAndPayDetailTab_New.BiZhiTabID = modelFinRecvMoneyDetailTab_NewOld.BiZhiTabID;

                Decimal dtFeeTotal = modelFinRecvMoneyDetailTab_NewOld.ShouxuFee
                    + modelFinRecvMoneyDetailTab_NewOld.LiXi
                    + modelFinRecvMoneyDetailTab_NewOld.BaoguanFee
                    + modelFinRecvMoneyDetailTab_NewOld.OtherFee;

                modelFinDepRecvAndPayDetailTab_New.WaiHuiIncomeGongShi
                    = modelFinRecvMoneyDetailTab_NewOld.YuanBiMoneyValue.ToString()
                    + "-" + dtFeeTotal.ToString() + @"/"
                    + modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv.ToString();

                if (modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv > 0)
                    modelFinDepRecvAndPayDetailTab_New.WaiHuiIncome
                        = modelFinRecvMoneyDetailTab_NewOld.YuanBiMoneyValue
                        - dtFeeTotal / modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv;
                else
                    modelFinDepRecvAndPayDetailTab_New.WaiHuiIncome = 0;

                modelFinDepRecvAndPayDetailTab_New.ToRMB = 0;

                modelFinDepRecvAndPayDetailTab_New.FactorA = 0;
                modelFinDepRecvAndPayDetailTab_New.FactorB = 0;
                modelFinDepRecvAndPayDetailTab_New.PayTaxBenJin = 0;
                modelFinDepRecvAndPayDetailTab_New.GuJiHuiLv = 0;
                modelFinDepRecvAndPayDetailTab_New.HuiLv = modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv;
                modelFinDepRecvAndPayDetailTab_New.ToFacMoney = modelFinDepRecvAndPayDetailTab_New.WaiHuiIncome
                    * modelFinDepRecvAndPayDetailTab_New.HuiLv;

                modelFinDepRecvAndPayDetailTab_New.PayTaxBenJinGongShi
                            = modelFinDepRecvAndPayDetailTab_New.WaiHuiIncome.ToString() + " * "
                            + modelFinDepRecvAndPayDetailTab_New.HuiLv.ToString();

                modelFinDepRecvAndPayDetailTab_New.DanJuDate = System.DateTime.Now.Date;
                modelFinDepRecvAndPayDetailTab_New.PayDate = System.DateTime.Now.Date;
                modelFinDepRecvAndPayDetailTab_New.RecvDate = modelFinRecvMoneyDetailTab_NewOld.JieHuiDate;
                modelFinDepRecvAndPayDetailTab_New.Note = "";

                modelFinDepRecvAndPayDetailTab_New.RevcShuiDanDate = modelFinRecvMoneyDetailTab_NewOld.RevcShuiDanDate;
                m_bllFinDepRecvAndPayDetailTab_New.Add(modelFinDepRecvAndPayDetailTab_New);
                Frm_Main.Log2DB(this.Text, "生成付款本金", modelFinRecvMoneyDetailTab_NewOld.BianHao);
            }
            catch
            {
                MessageBox.Show("没有选择记录，无法复制！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void 导出到付款本金ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UltraGridRow CurrentUGRFrom;
            CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];
            int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);
            ProducePayBenMoney(true, nSelectFinRecvMoneyDetailTab_NewID);
            Frm_Main.GetMainForm().ShowContent("部门收款及付款明细表管理");
        }

        private void 生成付款约定本金ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                UltraGridRow CurrentUGRFrom;
                CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);

                DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_NewOld = m_bllFinRecvMoneyDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);
                DataSet ds = m_bllFinDepRecvAndPayDetailTab_New.GetList("FinDepRecvAndPayDetailTab_New.RecvBianNo = '" + modelFinRecvMoneyDetailTab_NewOld.BianHao.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("已经生成了付款本金或者付款约定本金或者付款价税合计", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New = new DGBY.Model.FinDepRecvAndPayDetailTab_New();
                modelFinDepRecvAndPayDetailTab_New.RecvBianNo = modelFinRecvMoneyDetailTab_NewOld.BianHao;
                modelFinDepRecvAndPayDetailTab_New.FinDepRecvAndPayDetailTab_NewBianNo = MakePayMoneyDetailDJNo().ToString();
                modelFinDepRecvAndPayDetailTab_New.FactoryDataTabID = modelFinRecvMoneyDetailTab_NewOld.FactoryDataTabID;
                modelFinDepRecvAndPayDetailTab_New.FinRecvMoneyWayTabID = modelFinRecvMoneyDetailTab_NewOld.FinRecvMoneyWayTabID;
                modelFinDepRecvAndPayDetailTab_New.FinAbstractTabID = 5;//付款约定本金
                modelFinDepRecvAndPayDetailTab_New.BiZhiTabID = modelFinRecvMoneyDetailTab_NewOld.BiZhiTabID;

                Decimal dtFeeTotal = modelFinRecvMoneyDetailTab_NewOld.ShouxuFee
                    + modelFinRecvMoneyDetailTab_NewOld.LiXi
                    + modelFinRecvMoneyDetailTab_NewOld.BaoguanFee
                    + modelFinRecvMoneyDetailTab_NewOld.OtherFee;

                DataSet dsTemp = m_bllFactoryDataTab.GetList("FactoryDataTabid = '" + modelFinRecvMoneyDetailTab_NewOld.FactoryDataTabID + "'");
                Decimal dtDiscountHuilv = (Decimal)dsTemp.Tables[0].Rows[0]["优惠汇率"];
                if (modelFinRecvMoneyDetailTab_NewOld.BiZhiTabID == 1)//美元
                    modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv = modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv / (dtDiscountHuilv + 1);
                else if (modelFinRecvMoneyDetailTab_NewOld.BiZhiTabID == 2)//港币
                    modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv = modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv - dtDiscountHuilv;

                m_bllFinRecvMoneyDetailTab_New.Update(modelFinRecvMoneyDetailTab_NewOld);

                modelFinDepRecvAndPayDetailTab_New.WaiHuiIncomeGongShi
                    = modelFinRecvMoneyDetailTab_NewOld.YuanBiMoneyValue.ToString()
                    + "-" + dtFeeTotal.ToString() + @"/"
                    + modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv.ToString();

                if (modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv > 0)
                    modelFinDepRecvAndPayDetailTab_New.WaiHuiIncome
                        = modelFinRecvMoneyDetailTab_NewOld.YuanBiMoneyValue
                        - dtFeeTotal / modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv;
                else
                    modelFinDepRecvAndPayDetailTab_New.WaiHuiIncome = 0;

                modelFinDepRecvAndPayDetailTab_New.ToRMB = 0;
              //  modelFinDepRecvAndPayDetailTab_New.ToFacMoney = 0;
                modelFinDepRecvAndPayDetailTab_New.FactorA = 0;
                modelFinDepRecvAndPayDetailTab_New.FactorB = 0;
                modelFinDepRecvAndPayDetailTab_New.PayTaxBenJin = 0;
                modelFinDepRecvAndPayDetailTab_New.GuJiHuiLv = modelFinRecvMoneyDetailTab_NewOld.RMBHuiLv;
                modelFinDepRecvAndPayDetailTab_New.HuiLv = 0;
                modelFinDepRecvAndPayDetailTab_New.ToFacMoney = modelFinDepRecvAndPayDetailTab_New.WaiHuiIncome
       * modelFinDepRecvAndPayDetailTab_New.GuJiHuiLv;

                modelFinDepRecvAndPayDetailTab_New.PayTaxBenJinGongShi
                            = modelFinDepRecvAndPayDetailTab_New.WaiHuiIncome.ToString() + " * "
                            + modelFinDepRecvAndPayDetailTab_New.GuJiHuiLv.ToString();

                modelFinDepRecvAndPayDetailTab_New.DanJuDate = System.DateTime.Now.Date;
                modelFinDepRecvAndPayDetailTab_New.PayDate = System.DateTime.Now.Date;
                modelFinDepRecvAndPayDetailTab_New.RecvDate = modelFinRecvMoneyDetailTab_NewOld.JieHuiDate;
                modelFinDepRecvAndPayDetailTab_New.Note = "";
                modelFinDepRecvAndPayDetailTab_New.RevcShuiDanDate = modelFinRecvMoneyDetailTab_NewOld.RevcShuiDanDate;             

                m_bllFinDepRecvAndPayDetailTab_New.Add(modelFinDepRecvAndPayDetailTab_New);
                Frm_Main.Log2DB(this.Text, "生成付款约定本金", modelFinRecvMoneyDetailTab_NewOld.BianHao);


             

             
                Frm_Main.GetMainForm().ShowContent("部门收款及付款明细表管理");
            }
            catch
            {
                MessageBox.Show("没有选择记录，无法复制！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //private Decimal GetInvoiceRemain(string strWhere)
        //{
        //    DataSet ds = m_bllInvoiceAuxiliary_New.GetInvoiceRemain(strWhere);
        //    Decimal dtTotal = 0;
        //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        //    {
        //        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //        {
        //            if (ds.Tables[0].Rows[i]["InvoiceYuE"].ToString() != "")
        //            {
        //                Decimal dtTemp = Convert.ToDecimal(ds.Tables[0].Rows[i]["InvoiceYuE"].ToString());
        //                dtTotal += dtTemp;
        //            }
        //        }
        //    }
        //    return dtTotal;
        //}
        //private Decimal GetRecvMoneyRemainNew(string strWhere)
        //{
        //    DataSet ds = m_bllFinDepRecvAndPayDetailTab_New.GetRecvMoneyRemainNew(strWhere);
        //    Decimal dtTemp = 0;
        //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        //    {
        //        if (ds.Tables[0].Rows[0]["RecvYuE"].ToString() != "")
        //            dtTemp = Convert.ToDecimal(ds.Tables[0].Rows[0]["RecvYuE"].ToString());
        //    }
        //    return dtTemp;
        //}
        private Decimal GetRecvMoneyRemainNewRecv(string strWhere)
        {
            DataSet ds = m_bllFinRecvMoneyAuxiliaryTab_New.GetRecvMoneyRemain(strWhere);
            Decimal dtTemp = 0;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["RecvYuE"].ToString() != "")
                    dtTemp = Convert.ToDecimal(ds.Tables[0].Rows[0]["RecvYuE"].ToString());
            }
            return dtTemp;
        }
        //在处理多个退税点计算到厂货款的需求上有问题，未实现
        private void 生成付款价税合计ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
 
       
        private void ProcessRecvMoney(DGBY.Model.FinDepRecvAndPayDetailTab_New modelFinDepRecvAndPayDetailTab_New, Decimal dtOldPayTaxBenJin)
        {

        }


       
        private void buttonSumStart_Click(object sender, EventArgs e)
        {
            m_bSumStart = true;
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            for (int i = 0; i < ugr.Length; i++)
            {
                for (int j = 0; j < ugr[i].Cells.Count; j++)
                {
                    if (ugr[i].Cells[j].Appearance.BackColor == Color.Aqua)
                    {
                        ugr[i].Cells[j].Appearance.BackColor = Color.FromName("0");
                    }
                }
            }
        }

        private void buttonSumStop_Click(object sender, EventArgs e)
        {
            try
            {
                Decimal dtTotal = 0;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    for (int j = 0; j < ugr[i].Cells.Count; j++)
                    {
                        if (ugr[i].Cells[j].Appearance.BackColor == Color.Aqua)
                        {
                            Decimal dtTemp = Convert.ToDecimal(ugr[i].Cells[j].Value.ToString());
                            dtTotal += dtTemp;
                            //ugr[i].Cells[j].Appearance.BackColor = Color.FromName("0");
                        }
                    }
                }

                this.textBoxTotal.Text = dtTotal.ToString();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void ultraGrid1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                UltraGrid grid = (UltraGrid)sender;
                UIElement element = grid.DisplayLayout.UIElement.LastElementEntered;
                if (element == null)
                    return;
                Infragistics.Win.UltraWinGrid.ColumnHeader columnHeader = element.GetContext(typeof(Infragistics.Win.UltraWinGrid.ColumnHeader)) as Infragistics.Win.UltraWinGrid.ColumnHeader;
                if (columnHeader != null)
                {
                    string str = columnHeader.Caption;
                    if (str != "原币金额")
                        return;

                    Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                    if (ugr.Length <= 0)
                    {
                        return;
                    }
                    Decimal dtTemp = 0;
                    for (int i = 0; i < ugr.Length; i++)
                    {
                        string strTemp = ugr[i].Cells[str].Value.ToString();
                        if (strTemp != "")
                            dtTemp += Convert.ToDecimal(ugr[i].Cells[str].Value.ToString());
                    }
                    this.textBoxTotal.Text = dtTemp.ToString();




                }
                else
                {
                    // Set the grid's tooltip to null. 
                    //  Debug.WriteLine("null");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void buttonBacthMod_Click(object sender, EventArgs e)
        {
                        
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            if (ugr.Length <= 0)
            {
                MessageBox.Show("没有要批量修改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            for (int i = 0; i < ugr.Length; i++)
            {
                int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                DGBY.Model.FinRecvMoneyDetailTab_New modelFinRecvMoneyDetailTab_New
                    = m_bllFinRecvMoneyDetailTab_New.GetModel(nID);
                if(modelFinRecvMoneyDetailTab_New.BiZhiTabID == 1)//美元
                {
                    modelFinRecvMoneyDetailTab_New.RMBHuiLv = Convert.ToDecimal(this.textBoxUsdHuiLv.Text);
                    modelFinRecvMoneyDetailTab_New.HuiLvGongShi = "1";
                    modelFinRecvMoneyDetailTab_New.HuiLv = 1M;
                }
                else if (modelFinRecvMoneyDetailTab_New.BiZhiTabID == 2)//港币
                {
                    modelFinRecvMoneyDetailTab_New.RMBHuiLv = Convert.ToDecimal(this.textBoxHKHuiLv.Text);
                    modelFinRecvMoneyDetailTab_New.HuiLvGongShi = this.textBoxHKHuiLv.Text + "/" + this.textBoxUsdHuiLv.Text;
                    Decimal dtUsdHuiLv = Convert.ToDecimal(this.textBoxUsdHuiLv.Text);
                    if (dtUsdHuiLv > 0)
                        modelFinRecvMoneyDetailTab_New.HuiLv = Convert.ToDecimal(this.textBoxHKHuiLv.Text) / dtUsdHuiLv;
                    else
                        modelFinRecvMoneyDetailTab_New.HuiLv = 0;
                }
                else if (modelFinRecvMoneyDetailTab_New.BiZhiTabID == 5)//人民币
                {
                    modelFinRecvMoneyDetailTab_New.RMBHuiLv = 1;
                    modelFinRecvMoneyDetailTab_New.HuiLvGongShi = "1/" + this.textBoxUsdHuiLv.Text;
                    Decimal dtUsdHuiLv = Convert.ToDecimal(this.textBoxUsdHuiLv.Text);
                    if (dtUsdHuiLv > 0)
                        modelFinRecvMoneyDetailTab_New.HuiLv = 1 / dtUsdHuiLv;
                    else
                        modelFinRecvMoneyDetailTab_New.HuiLv = 0;
                }

                m_bllFinRecvMoneyDetailTab_New.Update(modelFinRecvMoneyDetailTab_New);
                Frm_Main.Log2DB(this.Text, "批量修改汇率", modelFinRecvMoneyDetailTab_New.HuiLv.ToString());
                MessageBox.Show("批量修改汇率成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            if (ugr.Length <= 0)
            {
                MessageBox.Show("没有要批量修改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DialogResult dr = MessageBox.Show("确实要批量生成以下" + ugr.Length.ToString() + "条记录的付款本金么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.Cancel)
                return;
            for (int i = 0; i < ugr.Length; i++)
            {
                int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                ProducePayBenMoney(false, nID);
            }
            Frm_Main.Log2DB(this.Text, "批量生成付款本金", "");
            Frm_Main.GetMainForm().ShowContent("部门收款及付款明细表管理");
            MessageBox.Show("批量生成付款本金成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
